/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.BorderLayout;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ro.dta.idbi.interfaces.IProject;
import ro.dta.idbi.interfaces.ui.IMainFrame;
import ro.dta.idbi.interfaces.ui.IProjectPane;
import ro.dta.idbi.interfaces.ui.IRightPanel;

/**
 * Project panel, used to guide the user in the integration process
 * 
 * @author Tudor Dobrila
 * 
 */
public class ProjectPanel extends JPanel implements IProjectPane {

	private static final long serialVersionUID = 3235408076721515257L;

	/**
	 * Parent frame of the application
	 */
	protected transient IMainFrame parentFrame;

	/**
	 * Project being modified in this panel
	 */
	protected transient IProject project;

	/**
	 * Center component displayed in the project panel
	 */
	protected transient JComponent centerComp;

	/**
	 * Parent panel of this panel
	 */
	protected final transient IRightPanel parentPanel;

	/**
	 * Project panel class constructor
	 * 
	 * @param parentFrame
	 *            Parent frame of the application
	 * @param project
	 *            Project being modified in this panel
	 */
	public ProjectPanel(final IMainFrame parentFrame, final IRightPanel parentPanel,
			final IProject project) {
		super();
		this.parentFrame = parentFrame;
		this.parentPanel = parentPanel;
		this.project = project;
		setLayout(new BorderLayout());

		changeCenter();
	}

	@Override
	public IProject getProject() {
		return project;
	}

	@Override
	public void next() {
		if (project.canAdvance()) {
			boolean lastStep = project.isLastStep();
			try {
				project.advance();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, "Could not advance the project.\r\nReason: "
						+ e.getMessage());
			}

			parentFrame.getRepositoryBrowser().refresh();
			if (!lastStep) {
				UiUtils.saveProject(parentFrame, this, project);
				changeCenter();
			} else {
				if (centerComp instanceof IntegrationPanel) {
					((IntegrationPanel)centerComp).disableProceedButton();
				}
				JOptionPane.showMessageDialog(this, "Integration of the schemas has been completed.");
			}
		}
	}

	/**
	 * Change the center panel depending on the status of the project
	 */
	private void changeCenter() {
		if (centerComp != null) {
			remove(centerComp);
		}

		if (project.getStrategy() == null) {
			// New project, show strategy diagram
			centerComp = new StrategyPanel(parentFrame, this);
			parentPanel.setTitle("Pre-integration: Choose Schemas & Define Integration Strategy");
		} else {
			if (project.getCurrentStep() == -1) {
				// Existing project, strategy definition phase
				centerComp = new StrategyPanel(parentFrame, this, project.getStrategy());
				parentPanel
						.setTitle("Pre-integration: Choose Schemas & Define Integration Strategy");
			} else {
				try {
					final String[] originalSchemas = project.getSchemas(project.getCurrentStep(),
							project.getCurrentOperation()).toArray(new String[] {});
					final String[] crtSchemas = project.getSchemasForCurrentStep().toArray(
							new String[] {});
					centerComp = new IntegrationPanel(parentFrame, this, originalSchemas,
							crtSchemas, project.getCurrentOperation());
					parentPanel.setTitle("Step " + (project.getCurrentStep() + 1) + ": Schema "
							+ project.getCurrentOperation().toString().toLowerCase());
				} catch (Exception e) {
					JOptionPane.showMessageDialog(
							this,
							"Could not continue with the integration project.\r\nReason: "
									+ e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
				}
			}
		}

		add(centerComp, BorderLayout.CENTER);
		revalidate();
		repaint();
	}
}
